<template>
  <a-modal :width="modalWidth"
           :title="title"
           :visible.sync="visible"
           @cancel="handleCancel"
           :footer="disableSubmit ? null : undefined">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
          <a-form-item label="表单名称" :labelCol="labelCol" :wrapperCol="wrapperCol" class="mt15">
              <a-input placeholder="请输入表单名称" v-decorator.trim="[ 'name', validatorRules.name]" :disabled="disableSubmit"/>
          </a-form-item>
          <a-form-item label="表单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-dict-select-tag  @change="onChangeFormType" :disabled="disableSubmit"
              placeholder="请选择表单类型" :triggerChange="true"
              dictCode="bpm_form_type" v-decorator="['type', validatorRules.type]"
            />
          </a-form-item>
          <a-form-item label="主表名" :labelCol="labelCol" :wrapperCol="wrapperCol" class="formModal-nodown">
            <j-select-table @change="changeMe" placeholder="请选择主表" :multiple="false"
                            v-decorator="['tableName', validatorRules.tableName]" :disabled="disableSubmit"/>
          </a-form-item>
          <a-form-item  v-if="localFormType=='zclx'" label="附表名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-select-table placeholder="请选择附表" :multiple="true" v-decorator="['assistTableName']" :disabled="disableSubmit"/>
          </a-form-item>
          <a-form-item label="表单描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator.trim="[ 'memo']" :disabled="disableSubmit"/>
          </a-form-item>
          <a-form-item label="是否发布" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-switch checkedChildren="是" unCheckedChildren="否" v-model="isPublish" :disabled="disableSubmit"/>
          </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button key="back"  @click="handleCancel">
        取消
      </a-button>
      <a-button key="submit" type="primary" :loading="confirmLoading"  @click="handleSubmit">
        提交
      </a-button>
    </template>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import moment from 'moment'
  import {addBpmForm,editBpmForm} from '@/api/api'
  import JSelectTable from '@/components/ksourcebiz/JSelectTable'

  export default {
    name: "FormModal",
    components: {
      JSelectTable
    },
    data () {
      return {
        modalWidth:800,
        id:"",
        isPublish:true,
        localFormType:"",
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
          validatorRules: {
              name: {rules: [{required: true, message: '请输入表单名称!'}]},
              type: {rules: [{required: true, message: '请选择表单类型!'}]},
              tableName: {rules: [{required: true, message: '请选择业务表!'}]},
          },
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        headers:{},
        confirmLoading: false,
        form:this.$form.createForm(this),
      }
    },
    methods: {
      refresh () {
          this.id=""
      },
      add () {
        this.refresh();
        this.edit({"id":this.id});
      },
      edit (record) {
        let that = this;
        that.form.resetFields();
        that.id = record.id;
        that.localFormType=record.type
        that.isPublish=record.publishState==2?false:true;
        that.visible = true;
        that.model = Object.assign({}, record);
        that.$nextTick(() => {
          if(that.localFormType=='dblx'){
            that.form.setFieldsValue(pick(this.model,'name','type','tableName','memo'));
          }else {
            that.form.setFieldsValue(pick(this.model,'name','type','tableName','assistTableName','memo'));
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      moment,
      handleSubmit () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            this.model.publishState = this.isPublish?1:2;
            let formData = Object.assign(this.model, values);
            let obj;
            if(!this.model.id){
              obj=addBpmForm(formData);
            }else{
              obj=editBpmForm(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },
      onChangeFormType(value) {
        this.localFormType=value
        /*this.$nextTick(() => {
          this.form.validateFields(['tableName'], { force: true });
        });*/
      },
      changeMe(data){
        console.log(data)
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader > .ant-upload {
    width:104px;
    height:104px;
  }
  .ant-upload-select-picture-card i {
    font-size: 49px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }

  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>